<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/18
 * Time: 22:00
 */

namespace App\Admin\Controllers;

use App\Admin\Actions\QueueBatchAction;
use App\Admin\Traits\UserTrait;
use App\Helper;
use App\Models\Circle;
use App\Models\GroupMessage;
use App\Models\Queue;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

/**
 * Class CircleController
 * @package App\Admin\Controllers
 * 朋友圈
 */
class CircleController extends AdminController
{
    use UserTrait;



    public function sended(Content $content)
    {
        $grid = $this->grid("status=1");
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->actions(function(Grid\Displayers\Actions $actions) {
            $actions->disableEdit();
            $actions->disableView();
        });
        return $content
            ->title("已发送记录")
            ->body($grid);
    }

    public function wait(Content $content)
    {
        $grid =  $this->grid("status=0");
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->actions(function(Grid\Displayers\Actions $actions) {
            $actions->disableEdit();
            $actions->disableView();
        });
        return $content
            ->title("待发送内容")
            ->body($grid);
    }

    public function multidelete($ids)
    {
        $ids = explode(",", $ids);
        if (empty($ids)) {
            return back();
        }
        $models = Circle::where("user_id", $this->getUserId())->whereIn("id", $ids)->get();
        if (empty($models->toArray())) {
            return back();
        }
        foreach ($models as $item) {
            if ($item->queue_id > 0) {
                Queue::query()->where("id", $item->id)->delete();
            }
            $item->delete();
        }
        return back();
    }


    public function grid($where)
    {
        $model = new Circle();
        $grid = new Grid($model);
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->model()->whereRaw($where)
            ->orderBy("id", "desc");
        Helper::gridLoadModel($grid, $model);

        $grid->batchActions(function ($batch) {
            $batch->add(new QueueBatchAction());
        });

        return $grid;
    }
}